package com.gylang.dict.controler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gylang.api.core.domain.AjaxResult;
import com.gylang.api.domain.EnumDictWrapper;
import com.gylang.common.core.controller.BaseController;
import com.gylang.common.core.page.TableDataInfo;
import com.gylang.common.utils.PageUtils;
import com.gylang.dict.repository.EnumDictRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.function.Predicate;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/sysDict")
public class SysEnumDictController extends BaseController {

	@Autowired
	private EnumDictRepository enumDictRepository;

	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@GetMapping("/list")
	public TableDataInfo<EnumDictWrapper> list(EnumDictWrapper dictWrapper) {
		Page<EnumDictWrapper> page = PageUtils.buildPage();

		Predicate<EnumDictWrapper> match = sysDictData -> {
			boolean matchCondition = true;
			if (StrUtil.isNotEmpty(dictWrapper.getName())) {
				matchCondition = sysDictData.getName().equals(dictWrapper.getName());
			}
			if (StrUtil.isNotEmpty(dictWrapper.getModule())) {
				matchCondition = matchCondition && sysDictData.getModule().equals(dictWrapper.getModule());
			}
			if (StrUtil.isNotEmpty(dictWrapper.getType())) {
				matchCondition = matchCondition && sysDictData.getType().equals(dictWrapper.getType());
			}
			return matchCondition;
		};
		return enumDictRepository.matchLimit(match, page.getCurrent(), page.getSize());
	}


	/**
	 * 查询字典数据详细
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:query')")
	@GetMapping(value = "/{dictCode}")
	public AjaxResult getInfo(@PathVariable String name) {
		return AjaxResult.success(enumDictRepository.findOne(name));
	}


}
